﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
using YArchitech.LIB;

namespace YArchitech.Temp
{
	internal class HYPipeSystemDocument
	{
		public HYPipeSystem PipeSystem { get; set; }

		public HYPipeSystemOption Option { get; set; }

		public string FileName { get; set; }

		public bool Modified { get; set; }

		public string Path { get; set; }

		public double Version
		{
			get
			{
				return (double)this.m_Version;
			}
		}

		public string ErrorString { get; set; }

		public HYPipeSpecifications Specifications
		{
			get
			{
				if (this.m_Specifications == null)
				{
					this.m_Specifications = HYDataAccess.GetPipeSpecifications();
				}
				return this.m_Specifications;
			}
		}

		public HYPipeSpecifications AvailableSpecifications
		{
			get
			{
				return this.m_AvailableSpecifications;
			}
		}

		public HYPipeSystemDocument()
		{
			this.Option = new HYPipeSystemOption();
			this.Option.Load(Product.UserConfigFile);
		}

		public bool Save()
		{
			if (string.IsNullOrEmpty(this.FileName))
			{
				this.ErrorString = "文件名为空！";
				return false;
			}
			bool result;
			try
			{
				XElement xelement = new XElement("HYPipeSystemDocument", new XAttribute("Version", this.m_Version));
				XElement xelement2 = new XElement("HYPipeSystemOption");
				this.Option.WriteXml(xelement2);
				xelement.Add(xelement2);
				XElement xelement3 = new XElement("HYPipeSystem");
				xelement.Add(xelement3);
				this.PipeSystem.WriteXml(xelement3);
				xelement.Save(this.FileName);
				this.ErrorString = "文件保存成功！";
				this.Modified = false;
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				this.ErrorString = "保存数据失败，请检查文件位置";
				result = false;
			}
			return result;
		}

		public bool Open()
		{
			if (string.IsNullOrEmpty(this.FileName) || File.Exists(this.FileName))
			{
				this.ErrorString = "文件不存在";
				return false;
			}
			bool result;
			try
			{
				XElement xelement = XElement.Load(this.FileName);
				XElement xe = xelement.Element("HYPipeSystemOption");
				this.Option.ReadXml(xe);
				XElement xe2 = xelement.Element("HYPipeSystem");
				this.PipeSystem.ReadXml(xe2);
				this.ErrorString = "打开文件成功！";
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				this.ErrorString = "读取文件错误，请确认数据文件是否完整！";
				result = false;
			}
			return result;
		}

		public bool FilterSpecifications()
		{
			if (this.m_Specifications == null || this.m_Specifications.Count == 0)
			{
				this.ErrorString = "没有水管数据，请首先设置水管管径！";
				return false;
			}
			this.m_AvailableSpecifications = this.m_Specifications.GetMatchSpecificationsByDNRange(this.Option.DNRange);
			if (this.m_AvailableSpecifications == null || this.m_AvailableSpecifications.Count == 0)
			{
				this.ErrorString = "公称直径范围设置不合理，过滤不到管径。请重新设置。";
				return false;
			}
			return true;
		}

		public bool OnDesign()
		{
			this.Option = null;
			this.Option = new HYPipeSystemOption();
			this.Option.Load(Product.UserConfigFile);
			if (!this.FilterSpecifications())
			{
				return false;
			}
			if (!this.PipeSystem.Design(this.Option, this.m_AvailableSpecifications))
			{
				this.ErrorString = this.PipeSystem.ErrorString;
				return false;
			}
			return true;
		}

		public bool OnCheck()
		{
			this.Option = null;
			this.Option = new HYPipeSystemOption();
			this.Option.Load(Product.UserConfigFile);
			if (!this.PipeSystem.Check(this.Option))
			{
				this.ErrorString = this.PipeSystem.ErrorString;
				return false;
			}
			return true;
		}

		public bool OnOpenFile()
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = this.Path;
			openFileDialog.Filter = "盈建科水系统计算文件|*.xml";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 0;
			openFileDialog.CheckFileExists = true;
			if (openFileDialog.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			string fileName = openFileDialog.FileName;
			string fileName2 = System.IO.Path.GetFileName(fileName);
			bool result;
			try
			{
				XElement xelement = XElement.Load(fileName);
				if (xelement == null || xelement.Name != this.XmlRootNode)
				{
					throw new Exception();
				}
				this.FileName = this.FileName;
				result = this.Open();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				this.ErrorString = fileName2 + "不是一个有效的盈建科水系统计算数据文件";
				result = false;
			}
			return result;
		}

		public bool OnSaveFile()
		{
			if (string.IsNullOrEmpty(this.FileName))
			{
				this.OnSaveAs();
			}
			return this.Save();
		}

		public bool OnSaveAs()
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.InitialDirectory = this.Path;
			saveFileDialog.Filter = "盈建科水系统计算文件|*.xml";
			saveFileDialog.RestoreDirectory = true;
			saveFileDialog.FilterIndex = 0;
			if (saveFileDialog.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			this.FileName = saveFileDialog.FileName;
			return this.Save();
		}

		public bool OnExcel(BackgroundWorker bkWorker = null)
		{
			HYPipeSystemExcelBook hypipeSystemExcelBook = new HYPipeSystemExcelBook(this.PipeSystem, bkWorker);
			if (hypipeSystemExcelBook.RunExcel())
			{
				this.ErrorString = "生成Excel计算书成功!";
				return true;
			}
			this.ErrorString = hypipeSystemExcelBook.ErrorString;
			return false;
		}

		public bool OnTxt()
		{
			return false;
		}

		public bool OnFormDestroy()
		{
			return false;
		}

		private readonly string XmlRootNode = "HYPipeSystem";

		private int m_Version = 1;

		private HYPipeSpecifications m_Specifications;

		private HYPipeSpecifications m_AvailableSpecifications;
	}
}
